/*
 * Created on Nov 14, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.recruiter.reporting;



import java.io.OutputStream;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Category;
import org.vectrics.VectricsException;
import org.vectrics.common.reporting.ReportManager;
import org.vectrics.common.reporting.pdf.BaseReport;
import org.vectrics.common.util.DateUtil;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;


/**
 * @author mmoore
 */
public class RecruitReportManager extends ReportManager {
	private static String COMPILE_ROOT_PATH = "custom/reports/";
	private static Category log = Category.getInstance(RecruitReportManager.class);
	
	/**
	 * 
	 */
	public RecruitReportManager(String database) {
		super(database);
	}

	
	public BaseReport createReport(Request request, OutputStream outStream) throws VectricsException {	
		log.debug("fillCodeReport()");
		BaseReport report = null;
		Domain domain = CoreServiceLocator.getUserAuthService().getSessionDomain(request);
		
		String reportType = request.getParameter("report");
		log.debug("Report type = " + reportType);
		if (reportType.equals("job")) {
			log.debug("Report type is Job Report");
			JobReport jobReport = new JobReport(domain, request.getLocale());
			String jobId = request.getParameter("jobId");
			if (jobId != null)
			    jobReport.setJobId(new Long(jobId));
			report = jobReport;
		} else if (reportType.equals("jobstatsummary")) {
				log.debug("Report type is Job Statistics Summary");
				JobStatisticSummaryReport jobStatisticSummaryReport = 
					new JobStatisticSummaryReport(domain, request.getLocale());
				report = jobStatisticSummaryReport;
		} else if (reportType.toLowerCase().equals("joblist")) {
			log.debug("Report type is Job List");
			report = new JobListReport(domain, request.getLocale());
			JobListReport jobListReport = (JobListReport)report;
			String activeParam = request.getParameter("activeOnly");
			if ((activeParam == null) || (activeParam.equals("true") == false)) {
				jobListReport.setActiveOnly(false);
			}
			else {
				jobListReport.setActiveOnly(true);
			}
			String companyGrouping = request.getParameter("companyGrouping");
			if (companyGrouping != null) {
				jobListReport.setGroupByCompany(true);
			}
			else {
				jobListReport.setGroupByCompany(true);
			}
			
		}
		else if (reportType.toLowerCase().equals("skillsetup")) {
			log.debug("Report type is Skill Setup");
			report = new SkillSetupReport(domain, request.getLocale());
		} else if (reportType.toLowerCase().equals("userevaluation")) {
			log.debug("Report type is User Evaluation Report");
			report = new UserSkillRatingsReport(domain, request.getLocale());
			String userId = request.getParameter("userId");
			String evaluationId = request.getParameter("evaluationId");
		
			UserSkillRatingsReport rpt = (UserSkillRatingsReport)report;
			rpt.setUserId(userId);
			rpt.setEvaluationId(evaluationId);
		} else if (reportType.toLowerCase().equals("candidate")) {
			log.debug("Report type is Candidate");
			report = new CandidateReport(domain, request.getLocale());
			String personId = request.getParameter("personId");
		
			CandidateReport rpt = (CandidateReport)report;
			rpt.setPersonId(personId);
		} else if (reportType.toLowerCase().equals("userlist")) {
			log.debug("Report type is User List");
			String userType = request.getParameter("userType");
			if (userType == null) 
				throw new VectricsException("Report: userList - requires userType parameter");
			if (userType.equals(UserListReport.EMPLOYEE_USER_TYPE)) {
				report = new EmployeeReport(domain, request.getLocale());
				EmployeeReport employeeReport = (EmployeeReport)report;
			} else {
				report = new UserListReport(domain, request.getLocale());
				UserListReport userListReport = (UserListReport)report;
				userListReport.setUserType(userType);
			}
		} else if (reportType.toLowerCase().equals("txlist")) {
			log.debug("Report type is TX List");
			report = new TransactionReport(domain, request.getLocale());
			int daysBack = 3;
			String daysBackParam = request.getParameter("daysBack");
			if (daysBackParam != null) {
				daysBack = Integer.parseInt(daysBackParam);
			} 
			TransactionReport txReport = (TransactionReport)report;
			Date now = new Date();
			Date endDate = new Date(now.getTime() + (1000 * 60 * 60 * 24) );
			Date startDate = new Date(endDate.getTime() - (daysBack * DateUtil.MILLIS_IN_A_DAY));
			txReport.setStartDate(startDate);
			txReport.setEndDate(endDate);
		}
		else if (reportType.toLowerCase().equals("usagereport")) {
			log.debug("Report type is Usage Report");
			report = new UsageReport(domain, request.getLocale());
			int daysBack = 3;
			String daysBackParam = request.getParameter("daysBack");
			if (daysBackParam != null) {
				daysBack = Integer.parseInt(daysBackParam);
			} 
			UsageReport usageReport = (UsageReport)report;
			Date now = new Date();
			Date endDate = new Date(now.getTime() + (1000 * 60 * 60 * 24) );
			Date startDate = new Date(endDate.getTime() - (daysBack * DateUtil.MILLIS_IN_A_DAY));
			usageReport.setStartDate(startDate);
			usageReport.setEndDate(endDate);
		}
		else {
			log.error("Error, do not know how to handle 'report' param = " + reportType);
			throw new VectricsException("Error, do not know how to handle 'report' param = " + reportType);
		} 
			
		return(report);
		
}	
	
	protected String getCompiledReportsPath() {
		String contextPath = getRequest().getContextPath();
		String path = null;
		if (getFormPath().toLowerCase().indexOf("candidatereport") >= 0) {
			path = COMPILE_ROOT_PATH + "CandidateReport.xml.cpl";
		}
		else if (getFormPath().toLowerCase().indexOf("joblist") >= 0) {
			path = COMPILE_ROOT_PATH + "JobListReport.xml.cpl";
			
		}
		else {
			String historyPath = COMPILE_ROOT_PATH + "JobHistoryReport.xml.cpl";
			path = COMPILE_ROOT_PATH + "JobReport.xml.cpl";
			getReportParameters().put("ReportTitle", "Job Report");
			String jobId = getRequest().getParameter("job");
			getReportParameters().put("JobId", jobId);
			getReportParameters().put("JobHistorySubreport", historyPath);
			
			log.info("History sub report = " + historyPath);
			log.info("getCompiledReportsPath() - preparing Job Report. Job = " + jobId);
		}
		
		log.info("getCompiledReportsPath() formPath = " + getFormPath() + "  path = " + path);
		return(path);
	}
	
	
	protected void setReportParameters(Map parameters) {
		
	}
	

}
